package com.palfund.butterknife;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by clvc on 2017/8/20.
 * 真正的速度是看不见的 !
 * Today is today , we will go !
 */

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    List<String> mData = new ArrayList<>();

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recyclerview,
                parent, false);
        return new RecyclerViewViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof RecyclerViewViewHolder) {
            RecyclerViewViewHolder viewHolder = (RecyclerViewViewHolder) holder;
            viewHolder.mTextView.setText(mData.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class RecyclerViewViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_item)
        TextView mTextView;

        public RecyclerViewViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    //----------------------------------------------------------------------------------------------
    //更新适配器中的数据

    /**
     * 适配器中添加单条数据，刷新UI
     *
     * @param position 要添加的数据所在适配器中的位置
     * @param item     要添加的数据
     */
    public void insertItem(int position, String item) {
        mData.add(position, item);
        notifyItemInserted(position);
    }

    /**
     * 适配器中批量添加数据，刷新UI
     *
     * @param data          批量添加的集合
     * @param positionStart 添加到适配器中的起始位置
     */
    public void insertItemRange(int positionStart, List<String> data) {
        mData.addAll(positionStart, data);
        int itemCount = data.size();
        notifyItemRangeInserted(positionStart, itemCount);
    }

    /**
     * 适配器中删除单条数据，刷新UI
     *
     * @param position 要删除的数据所在适配器中的位置
     */
    public void removeItem(int position) {
        mData.remove(position);
        notifyItemRemoved(position);
    }

    /**
     * 适配器中批量删除多条数据，刷新UI
     *
     * @param positionStart 删除的起始位置
     * @param itemCount     删除的条数
     */
    public void removeItemRange(int positionStart, int itemCount) {
        for (int i = positionStart; i < (positionStart + itemCount); i++) {
            mData.remove(positionStart);
        }
        //所有的和detaDelete数据一样的都会删除掉
        //mData.removeAll(dataDelete);
        notifyItemRangeRemoved(positionStart, itemCount);
    }

    /**
     * 更新适配器中单条数据，刷新UI
     *
     * @param position 更新的数据所在适配器中的位置
     * @param item     更新后的数据
     */
    public void updateItem(int position, String item) {
        mData.remove(position);
        mData.add(position, item);
        notifyItemChanged(position);
    }

    /**
     * 批量更新适配器中的数据
     *
     * @param data          更新后的数据
     * @param positionStart 更新开始的位置
     */
    public void updateItemRange(int positionStart, List<String> data) {
        int itemCount = data.size();
        for (int i = positionStart; i < (positionStart + itemCount); i++) {
            mData.remove(positionStart);
        }
        mData.addAll(positionStart, data);
        notifyItemRangeChanged(positionStart, itemCount);
    }

    /**
     * @param fromPosition 移动前的位置
     * @param toPosition   移动后的位置
     */
    public void itemMoved(int fromPosition, int toPosition) {
        String data = mData.get(fromPosition);
        mData.remove(fromPosition);
        mData.add(toPosition, data);
        notifyItemMoved(fromPosition, toPosition);
    }

    public void addAll(List<String> data, boolean cleared) {
        if (cleared) {
            mData.clear();
            notifyDataSetChanged();
        }
        insertItemRange(mData.size(), data);
    }


}
